﻿using System;
using MS.InformationManagement.Presenter.Support;
using MS.InformationManagement.Presenter.UserProfileCreation;
using MS.InformationManagement.Support;
using System.Linq;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;

namespace MS.InformationManagement.WP.DocumentCenter.UserProfileCreation
{
    public partial class UserProfileCreationUserControl : BaseControl, IUserProfileCreationView
    {
        #region [Private Fields]

        UserProfileCreationPresenter _Presenter;

        #endregion

        #region [Event Handlers]

        protected void btnDelete_DataBinding(object sender, EventArgs e)
        {
            ImageButton trigger = sender as ImageButton;
            trigger.OnClientClick = string.Format("return confirm('Esta seguro de que desea eliminar el perfil {0}?')", trigger.OnClientClick);
        }

        protected void btnUpgrade_Click(object sender, EventArgs e)
        {
            try
            {
                _Presenter.UpgradeProfile();
                pnlUpgradeProfile.Visible = false;
                chkIsSiteContributor.Checked = false;
                HandleSuccess("El perfil ha sido actualizado exitosamente", null);
                useCreationForm.Visible = true;
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        protected void btnEdit_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            try
            {
                ImageButton trigger = sender as ImageButton;
                ProfileID = Convert.ToInt32(trigger.CommandArgument);
                _Presenter.LoadProfile();
                useCreationForm.Visible = false;
                pnlUpgradeProfile.Visible = true;
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        protected void btnDelete_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            try
            {
                ImageButton trigger = sender as ImageButton;
                ProfileID = Convert.ToInt32(trigger.CommandArgument);
                _Presenter.DeleteProfile();
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            try
            {
                _Presenter.CreateNewProfile();
                _Presenter.HandleLoad();
                HandleSuccess("El nuevo perfil ha sido creado exitosamente.", null);
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        protected void btnFromAvailableToSelectedAll_Click(object sender, EventArgs e)
        {
            try
            {
                _Presenter.SelectAllRoles();
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        protected void btnFromSelectedToAvailableJustSelected_Click(object sender, EventArgs e)
        {
            try
            {
                _Presenter.FromSelectedToAvailable();
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        protected void btnFromSelectedToAvailableAll_Click(object sender, EventArgs e)
        {
            try
            {
                _Presenter.UnSelectAllRoles();
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        protected void btnFromAvailableToSelectedJustSelected_Click(object sender, EventArgs e)
        {
            try
            {
                _Presenter.FromAvailableToSelected();
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        protected void btnUpgradeFromAvailableToSelectedJustSelected_Click(object sender, EventArgs e)
        {
            try
            {
                _Presenter.FromAvailableToSelectedUpgrade();
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }    
        }

        protected void btnUpgradeFromAvailableToSelectedAll_Click(object sender, EventArgs e)
        {
            try
            {
                _Presenter.SelectAllRolesUpgrade();
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        protected void btnUpgradeFromSelectedToAvailableJustSelected_Click(object sender, EventArgs e)
        {
            try
            {
                _Presenter.FromSelectedToAvailableUpgrade();
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        protected void btnUpgradeFromSelectedToAvailableAll_Click(object sender, EventArgs e)
        {
            try
            {
                _Presenter.UnSelectAllRolesUpgrade();
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try { _Presenter.HandleLoad(); }
                catch (Exception ex) { HandleError(ex); }
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Page.RegisterAjaxLoaderPlugin();
            _Presenter = new UserProfileCreationPresenter(this);
            //Cache[string.Format("{0}_{1}", "UpgradeAvailableRoles", SPContext.Current.Web.CurrentUser.LoginName)] = new System.Collections.Generic.List<DAL.DocumentCenter.Roles>();
            txtbProfileName.Required("El nombre del perfil es de caracter obligado", "vgCreateProfile", AjaxControlToolkit.ValidatorCalloutPosition.BottomRight);
            txtbUpgradeProfileName.Required("El nombre del perfil es de caracter obligado", "vgUpdateProfile", AjaxControlToolkit.ValidatorCalloutPosition.BottomRight);
            btnCreate.AddLoader();
            btnFromAvailableToSelectedAll.AddLoader();
            btnFromAvailableToSelectedJustSelected.AddLoader();
            btnFromSelectedToAvailableAll.AddLoader();
            btnFromSelectedToAvailableJustSelected.AddLoader();
            btnUpgrade.AddLoader();
            btnUpgradeFromSelectedToAvailableAll.AddLoader();
            btnUpgradeFromAvailableToSelectedJustSelected.AddLoader();
            btnUpgradeFromSelectedToAvailableAll.AddLoader();
            btnUpgradeFromSelectedToAvailableJustSelected.AddLoader();
  
        }

        protected void btnCancelar_Click(object sender, EventArgs e)
        {
            pnlUpgradeProfile.Visible = false;
            useCreationForm.Visible = true;
        }

        #endregion

        #region [IUserProfileCreationView Members]

        public System.Collections.Generic.List<DAL.DocumentCenter.Roles> UpgradeAvailableSelectedRoles
        {
            get
            {
                return (from ListItem itemRole in lstUpgradeAvailableRoles.Items
                        join role in UpgradeAvailableRoles
                        on Convert.ToInt32(itemRole.Value) equals role.Id
                        where itemRole.Selected
                        select role).ToList();
            }
        }

        public System.Collections.Generic.List<DAL.DocumentCenter.Roles> UpgradeSelectedSelectedRoles
        {
            get
            {
                return (from ListItem itemRole in lstUpgradeSelectedRoles.Items
                        join role in UpgradeSelectedRoles
                        on Convert.ToInt32(itemRole.Value) equals role.Id
                        where itemRole.Selected
                        select role).ToList();
            }
        }

        public System.Collections.Generic.List<DAL.DocumentCenter.Roles> UpgradeAvailableRoles
        {
            get
            {
                return Cache[string.Format("{0}_{1}", "UpgradeAvailableRoles", SPContext.Current.Web.CurrentUser.LoginName)] as System.Collections.Generic.List<DAL.DocumentCenter.Roles>;
            }
            set
            {
                Cache[string.Format("{0}_{1}", "UpgradeAvailableRoles", SPContext.Current.Web.CurrentUser.LoginName)] = value;
                lstUpgradeAvailableRoles.Items.Clear();
                lstUpgradeAvailableRoles.DataSource = value;
                lstUpgradeAvailableRoles.DataBind();
            }
        }

        public System.Collections.Generic.List<DAL.DocumentCenter.Roles> UpgradeSelectedRoles
        {
            get
            {
                return Cache[string.Format("{0}_{1}", "UpgradeSelectedRoles", SPContext.Current.Web.CurrentUser.LoginName)] as System.Collections.Generic.List<DAL.DocumentCenter.Roles>;
            }
            set
            {
                Cache[string.Format("{0}_{1}", "UpgradeSelectedRoles", SPContext.Current.Web.CurrentUser.LoginName)] = value;
                lstUpgradeSelectedRoles.Items.Clear();
                lstUpgradeSelectedRoles.DataSource = value;
                lstUpgradeSelectedRoles.DataBind();
            }
        }

        public string UpgradeProfileName
        {
            get { return txtbUpgradeProfileName.Text; }
            set { txtbUpgradeProfileName.Text = value; }
        }

        public int ProfileID
        {
            get { return Convert.ToInt32(hdfProfileID.Value); }
            set { hdfProfileID.Value = value.ToString(); }
        }

        public System.Collections.Generic.List<DAL.DocumentCenter.Roles> AvailableSelectedRoles
        {
            get
            {
                return (from ListItem itemRole in lstbAvailableRoles.Items
                        join role in AvailableRoles
                        on Convert.ToInt32(itemRole.Value) equals role.Id
                        where itemRole.Selected
                        select role).ToList();
            }
        }

        public System.Collections.Generic.List<DAL.DocumentCenter.Roles> SelectedSelectedRoles
        {
            get
            {
                return (from ListItem itemRole in lstSelectedRoles.Items
                        join role in SelectedRoles
                        on Convert.ToInt32(itemRole.Value) equals role.Id
                        where itemRole.Selected
                        select role).ToList();
            }
        }

        public System.Collections.Generic.List<DAL.DocumentCenter.Roles> AvailableRoles
        {
            get
            {
                return Cache[string.Format("{0}_{1}", "AvailableRoles", SPContext.Current.Web.CurrentUser.LoginName)] == null ? default(System.Collections.Generic.List<DAL.DocumentCenter.Roles>) :
                    Cache[string.Format("{0}_{1}", "AvailableRoles", SPContext.Current.Web.CurrentUser.LoginName)] as System.Collections.Generic.List<DAL.DocumentCenter.Roles>;
                //return lstbAvailableRoles.DataSource as System.Collections.Generic.List<DAL.DocumentCenter.Roles>;
            }
            set
            {
                Cache[string.Format("{0}_{1}", "AvailableRoles", SPContext.Current.Web.CurrentUser.LoginName)] = value;
                lstbAvailableRoles.Items.Clear();
                lstbAvailableRoles.DataSource = value;
                lstbAvailableRoles.DataBind();
            }
        }

        public System.Collections.Generic.List<DAL.DocumentCenter.Roles> SelectedRoles
        {
            get
            {
                return Cache[string.Format("{0}_{1}", "SelectedRoles", SPContext.Current.Web.CurrentUser.LoginName)] == null ? default(System.Collections.Generic.List<DAL.DocumentCenter.Roles>) :
                    Cache[string.Format("{0}_{1}", "SelectedRoles", SPContext.Current.Web.CurrentUser.LoginName)] as System.Collections.Generic.List<DAL.DocumentCenter.Roles>;
                //return lstSelectedRoles.DataSource as System.Collections.Generic.List<DAL.DocumentCenter.Roles>;
            }
            set
            {
                Cache[string.Format("{0}_{1}", "SelectedRoles", SPContext.Current.Web.CurrentUser.LoginName)] = value;
                lstSelectedRoles.Items.Clear();
                lstSelectedRoles.DataSource = value;
                lstSelectedRoles.DataBind();
            }
        }

        public string ProfileName
        {
            get
            {
                return txtbProfileName.Text;
            }
            set
            {
                txtbProfileName.Text = value;
            }
        }

        public System.Collections.Generic.IEnumerable<DAL.DocumentCenter.VincularPerfiles> Profiles
        {
            get
            {
                return dtlProfiles.DataSource as System.Collections.Generic.IEnumerable<DAL.DocumentCenter.VincularPerfiles>;
            }
            set
            {
                dtlProfiles.DataSource = value;
                dtlProfiles.DataBind();
            }
        }

        public bool IsSiteContributor
        {
            get
            {
                return chkIsSiteContributor.Checked;
            }
        }

        public bool IsSiteContributorEdit
        {
            get
            {
               return ChkEditIsContribute.Checked;
            }
            set
            {
                ChkEditIsContribute.Checked = value;
            }
        }


        #endregion            
    }
}
